package com.zhou.base.components.bs.common.properties.convert;

import org.springframework.boot.context.properties.ConfigurationPropertiesBinding;
import org.springframework.format.Formatter;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.Locale;

/**
 * @author xianfeng
 * @date 2023/7/26 11:31
 */
@Component
@ConfigurationPropertiesBinding
public class YearMonthFormatter implements Formatter<YearMonth> {

    private static DateTimeFormatter YEAR_MONTH_FORMATTER = DateTimeFormatter.ofPattern("yyyyMM");

    @Override
    public YearMonth parse(String text, Locale locale) throws ParseException {
        YearMonth target = YearMonth.parse(text, YEAR_MONTH_FORMATTER);
        return target;
    }

    @Override
    public String print(YearMonth object, Locale locale) {
        return object.toString();
    }

}
